package com.silong.swing.myWindows;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by 龙哥 on 2022-01-17 15:38
 * Silong Software.
 */

public class MyFrameInnerClass extends JFrame {

    JButton button1;
    JButton button2;

    public MyFrameInnerClass(){
        init();
    }

    private void init(){

        button1 = new JButton("按钮一");
        button2 = new JButton("按钮二");

        Container window = this.getContentPane();
        this.setSize(800, 600);
        this.setLocationRelativeTo(null); //距中
        this.setTitle("自定义控件大小位置");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //退出窗口关闭程序

        //设置布局为空，优点:摆放灵活，缺点：代码量大，自适应能力差。
        this.setLayout(null);

        //使用普通内部类添加监听
        //为普通内部类添加属性
        class ActionPerformceImpl implements ActionListener{
            int count = 0; //统计次数
            int num;
            ActionPerformceImpl(int num){
                this.num = num;
            }
            @Override
            public void actionPerformed(ActionEvent e) {
                ++count;
                button2.setText("按钮" + num + "点击" + count + "次");
            }
        }
        //自定义按钮的位置 ，大小
        button1.setLocation(100,50); //设置按钮位置
        button1.setSize(200,50); //设置按钮大小
        //给 button1添加普通内部类添加监听器
        button1.addActionListener(new ActionPerformceImpl(1));

        button2.setBounds(400, 50, 200, 50); //一次性设置位置与大小
        //给 button2添加普通内部类添加监听器
        button2.addActionListener(new ActionPerformceImpl(2));

        window.add(button1);
        window.add(button2);
    }
}
